<template>
    <div style="width: 100%;height: 100vh;overflow:hidden;">
    <div style="width: 300px;margin: 150px auto;">
      <div style="color: black; padding: 30px 0; font-size: 30px;text-align: center">欢迎登录</div>

      <el-form ref="formRef" :model="form" size="normal" :rules="rules">
        <el-form-item prop="username" style="height: 50px">
          <el-input placeholder="请输入用户名" v-model="form.username" style="height: 50px"></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input placeholder="请输入密码" type="password" v-model="form.password" style="height: 50px;margin-top: 10px"></el-input>
        </el-form-item>

        <el-form-item>
          <div style="display: flex;height: 45px;margin-top: 10px">
            <el-input placeholder="请输入验证码" prefix-icon="el-icon-key" v-model="form.validCode"
                      style="width: 60%;margin-right: 15px">
            </el-input>
            <div style="transform: scale(1.1)">
              <ValidCode @input="createValidCode"/>
            </div>
          </div>
        </el-form-item>

        <el-form-item style="text-align: center">
          <div style="margin-left: 22%;height: 25px;margin-top: -5px;">
            <el-radio v-model="role" :label="1">管理员</el-radio>
            <el-radio v-model="role" :label="2">普通用户</el-radio>
          </div>
        </el-form-item>

        <el-form-item>
          <el-button style="width: 100%;height: 45px" @click="login" type="primary">登录</el-button>
        </el-form-item>

        <el-form-item>
          <el-button style="width: 100%;height: 45px;background-color: white;color: #000" @click="toRegister" type="primary">还没有账户？立即注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>

</template>

<script>
import request from "@/utils/request";
import ValidCode from '@/components/ValidCode'

export default {
  name: "Login",
  components: {
    ValidCode
  },
  data() {
    return {
      form: {role:1},
      validCode:"",
      role:1,
      rules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur',
          },
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur',
          },
        ],
      }
    }
  },
  methods: {
    createValidCode(data){
      this.validCode = data
    },
    toRegister(){
      console.log("跳转")
      this.$router.push("/register")
    },
    login() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          //比较验证码
          if (!this.form.validCode)
          {
            this.$message.error("请输入验证码")
            return
          }
          if (this.form.validCode.toLowerCase()!== this.validCode.toLowerCase())
          {
            this.$message.error("验证码错误")
            return;
          }
          request.post("/user/login", this.form).then(res => {
            console.log(this.form)
            if (res.code === '0') {
              this.$message({
                type: "success",
                message: "登陆成功"
              })
              if (res.data.role !== this.role) {
                console.log("选择：" + this.role)
                console.log(res.data)
                console.log("实际：" + res.data.role)
                this.$message.error("没有该权限")
                return
              }
              sessionStorage.setItem("user",JSON.stringify(res.data))
              this.$router.push("/")
            } else {
              this.$message({
                type: "error",
                message: res.msg
              })
            }
          })
        }
      })

    }
  }
}

</script>

<style scoped>

</style>